from sqlalchemy import and_
from sqlalchemy.orm import Session

from backend.db.database import SessionLocal
from backend.models.member import Member, MemberCertificate, CertificatesStatus
from backend.schemas.member_pydantic import MemberCertificateResponse


class CertificateServices:
    """存单记录服务类"""

    def __init__(self, session: Session):
        self.session = session

    def add_certificate(self, member_id, certificates, certificates_status=CertificatesStatus.NO):
        """添加存单类
        返回MemberCertificate的orm对象
        """
        member = self.session.query(Member).filter(Member.id == member_id).first()
        if not member:
            raise ValueError(f"会员不存在 (ID: {member_id})")
        member_certificate = MemberCertificate(member_id=member_id,
                                               certificates=certificates,
                                               certificates_status=certificates_status)
        self.session.add(member_certificate)
        self.session.commit()
        self.session.refresh(member_certificate)
        return member_certificate

    def change_certificate(self, member_id, certificate_id):
        """修改存单状态
        已消费！！
        返回MemberCertificate的orm对象
        """
        member_certificate = self.session.query(MemberCertificate).filter(and_(MemberCertificate.member_id == member_id,
                                                                               MemberCertificate.id == certificate_id)).first()
        if not member_certificate:
            raise ValueError(f"存单不存在 (member_id: {member_id}, certificate_id: {certificate_id})")
        member_certificate.certificates_status = CertificatesStatus.YES
        self.session.commit()
        self.session.refresh(member_certificate)
        return member_certificate

    def get_certificates(self, member_id=None):
        """获得单个会员的所有存单记录以及数量"""
        if member_id:
            member_certificate = self.session.query(MemberCertificate).filter(
                MemberCertificate.member_id == member_id).all()
        else:
            member_certificate = self.session.query(MemberCertificate).all()
        certificate_pydantic = [MemberCertificateResponse(id=one.id,
                                                          member_id=one.member_id,
                                                          certificates=one.certificates,
                                                          certificates_status=one.zh_cn_display,
                                                          created_at=one.created_at) for one in member_certificate]

        return certificate_pydantic

    def get_not_consumed(self):
        member_certificate = self.session.query(MemberCertificate).filter(
            MemberCertificate.certificates_status == CertificatesStatus.NO).all()
        certificate_pydantic = [MemberCertificateResponse(id=one.id,
                                                          member_id=one.member_id,
                                                          certificates=one.certificates,
                                                          certificates_status=one.zh_cn_display,
                                                          created_at=one.created_at) for one in member_certificate]
        return certificate_pydantic
if __name__ == '__main__':
    with SessionLocal() as se:
        print(se)
        a = CertificateServices(se)
        b = a.get_certificates()
        for i in b:
            print(i.id)
